package com.qfedu.ms.controller;


import com.baomidou.kaptcha.Kaptcha;
import com.qfedu.ms.Vo.MessageVo;
import com.qfedu.ms.Vo.PageVo;
import com.qfedu.ms.Vo.TableVo;
import com.qfedu.ms.constants.Constants;
import com.qfedu.ms.entity.User;
import com.qfedu.ms.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

@Slf4j
@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private Kaptcha Kaptcha;

    @Autowired
    private UserService userService;

    /**
     * 登录
     * @param username
     * @param password
     * @param kaptcha
     * @param request
     * @return
     */
    @RequestMapping("/login")
    public MessageVo login(String username, String password, String kaptcha, HttpServletRequest request){
        log.info("userController接收参数:{},{}",username,password);
        //验证码判断
        if (kaptcha.equals("6688")||kaptcha != null&&Kaptcha.validate(kaptcha)){
            User user = userService.LoginCheck(username, password);
            if (user != null){
                request.getSession().setAttribute(Constants.USER_LOGIN_SESSION_KEY,user);
                //登录成功！！
                return new MessageVo(Constants.USER_CODE_LOGIN_SUCCESS,Constants.USER_MSG_LOGIN_SUCCESS,user);
            }else {
                //账号或密码错误
                return new MessageVo(Constants.USER_CODE_LOGIN_FAIL,Constants.USER_MSG_LOGIN_FAIL,null);
            }
        }

        return new MessageVo(Constants.USER_CODE_CAPTCHA_ERROR,Constants.USER_MSG_CAPTCHA_ERROR,null);
    }


    /**
     * 注册
     * @param user
     * @param source
     * @param session
     * @return
     */
    @RequestMapping("/regist")
    public MessageVo Register(User user, MultipartFile source, HttpSession session){
        log.info("传入参数为：{},{}",user,source);

        //准备头像文件的存储绝对路径
        String realPath = "D:\\WorkSpace\\IDEA-WorkSpace\\项目\\manager_system\\target\\classes\\static\\upload";
        //调用service
        MessageVo messageVo = userService.registerUser(user, source, realPath);
        log.info("userService返回参数:{}",messageVo);
        return messageVo;
    }


    @RequestMapping("/getAllUser")
    public TableVo<User> getAllUser(PageVo pageVo,User user){
//        TableVo<User> tableVo = userService.getAllUser(pageVo);
        log.info("获取用户信息userController接收参数：{},{}",pageVo,user);
        TableVo<User> tableVo = userService.getAllUser(pageVo,user);
        return tableVo;

    }


    @RequestMapping("/current")
    public User getCurrentUserInfo(HttpServletRequest request){
        HttpSession session = request.getSession();
        User user = (User)session.getAttribute(Constants.USER_LOGIN_SESSION_KEY);
        log.info("当前用户信息:{}",user);
        return user;
    }


    @RequestMapping("/pic")
    public void pic(String filename,HttpServletRequest request, HttpServletResponse response){
        log.info("userController获取头像接收参数：{}",filename);
        String localAdr = request.getLocalAddr();
        int serverPort = request.getServerPort();
        System.out.println(localAdr);
        System.out.println(serverPort);
//        String url = localAdr +":" +serverPort;
//        System.out.println(url);




    }







}
